// pages/route/addpoi/addpoi.js
const app = getApp()
const http = require("../../../utils/http.js")
const utils = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WindowHeight: app.globalData.WindowHeight,

    values: [],
    index: 0,

    route: {},
    mddlist: [],

    checkList: [],
    checkedList: [],

    name: '',

    isShow: false,


    version: '1.0.0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    http.get("/get")
      .then(res => {
        that.setData({
          version: res.data
        })
      })

    let routeId = options.routeId
    // let routeId = 6

    http.get("/route/getDetail", {
      routeId: routeId
    }).then(res => {
      let routeData = res.data.data;
      let startDayDate = utils.dateFormat(routeData.route.startDayDate)
      routeData.route.startDayDate = startDayDate
      for (let i = 0; i < routeData.routeDay.length; i++) {
        let date = utils.dateFormat(routeData.routeDay[i].routeDay.dayDate)
        routeData.routeDay[i].routeDay.dayDate = date
      }
      that.setData({
        route: routeData
      })
    })

    http.get("/routeMdd/getByRouteId", {
      routeId: routeId
    }).then(res => {
      let mddlist = res.data.data
      let values = []
      for (let i = 0; i < mddlist.length; i++) {
        let list = []
        values.push(mddlist[i].mddName)
        that.data.checkList.push(list)
        that.setData({
          checkList: that.data.checkList
        })
      }
      if (mddlist.length != 0) {
        http.post("/poi/getPois", {
          mddId: mddlist[0].mddId,
          name: that.data.name
        }).then(result => {
          let checkList = that.data.checkList
          checkList[0] = result.data.data
          that.setData({
            checkList: checkList
          })
        })
      }
      that.setData({
        values: values,
        mddlist: res.data.data
      })
    })
  },

  choosebtn: function(e) {
    this.setData({
      isShow: true
    })
  },

  choosemdd: function(e) {
    let that = this
    let index = e.currentTarget.dataset.item
    that.setData({
      isShow: false,
      index: index
    })

    let mddlist = that.data.mddlist
    http.post("/poi/getPois", {
      mddId: mddlist[index].mddId,
      name: that.data.name
    }).then(result => {
      let checkList = that.data.checkList
      checkList[index] = result.data.data
      that.setData({
        checkList: checkList
      })
    })
  },
  inputChange(e) {
    let that = this
    that.setData({
      name: e.detail.value
    })

    let mddlist = that.data.mddlist
    let index = that.data.index
    http.post("/poi/getPois", {
      mddId: mddlist[index].mddId,
      name: that.data.name
    }).then(result => {
      let checkList = that.data.checkList
      checkList[index] = result.data.data
      that.setData({
        checkList: checkList
      })
    })
  },

  choosechange(e) {
    let that = this
    let checkList = that.data.checkList
    let index = that.data.index
    let checkedList = []
    for (let i = 0; i < checkList[index].length; i++) {
      for (let j = 0; j < e.detail.value.length; j++) {
        if (checkList[index][i].poiId == e.detail.value[j]) {
          let poi = {
            poiId: checkList[index][i].poiId,
            poiName: checkList[index][i].name,
            lat: checkList[index][i].lat,
            lng: checkList[index][i].lng
          }
          checkedList.push(poi)
        }
      }
    }
    
    that.setData({
      checkedList: checkedList
    })
  },


  addRoutePoi(e){
    let checkedList = this.data.checkedList
    if (checkedList.length == 0){
      return;
    }
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  submit(e){
    let checkedList = this.data.checkedList
    let routerDayId = e.currentTarget.dataset.id
    for(let i=0;i<checkedList.length;i++){
      checkedList[i].routerDayId = routerDayId
    }

    let param = {
      checkedList: checkedList
    }
    http.post("/routeDayPoi/insert",{
      param: JSON.stringify(param)
    }).then(res=>{
      if(res.data.status == 200){
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})